//主价格省份
dongtai()
function dongtai(keyword) {
    $.ajax({
        contentType: "application/json;charset=UTF-8",
        url:root.sroreinter.getAddressByMainAddressList,
        cache: false, //禁用缓存
        type: "POST",
        dataType:"json",
        async:false,//优先运行
        data: JSON.stringify({
            addressCode:"",
            addressCodes:[],
            parentAddressCode:"",
            parentAddressCodes:[],
            addressLevels:['101'],
            addressNameLike:keyword,//模糊搜索关键字
            pageSize:1000,
            pageNum:1

        }),
        success: function (data) {
            console.log(data)
            var optionstring = "";
            if(data.data.length >0){
                for(var i=0;i<data.data.length;i++){
                    pl=data.data
                    optionstring += "<li class='quyuss' onclick='addCodeFun2("+  data.data[i].code+ ",\""+ data.data[i].name+ "\")'>"+ data.data[i].name+ "</li>";
    
                    $("#getArea").html(optionstring);
                }
            }else{
                $("#getArea").html('暂无数据');  
                $("#areaId").attr('porvinname','')
                $("#areaId").attr('porvincode','')
            }

        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });
};
$("#fen_lsit").focus(function () {
    $("#getArea").show()
});
$(document).bind("click",function(e){
    if($(e.target).closest("#getArea").length == 0 && $(e.target).closest("#fen_lsit").length == 0){
        $("#getArea").hide()
    }
})
function addCodeFun2(id,brand_name) {
    $("#getArea").hide();
    $("#fen_lsit").val(brand_name)
    $("#areaId").attr('porvincode',id)
    $("#areaId").attr('porvinname',brand_name)
};
function seachprovicnsfn(e){
    dongtai($(e).val())
}
/*----------------------区域获取开始------------------------*/
function selectArea() {
    if($("#areaName").html()){
        $("#fen_lsit").val($("#areaName").html())
    }
    $("#selectInp").css({
        display: "block"
    });
}


function area_sure() {
    if($("#areaId").attr('porvincode') == ""){
        layer.msg('请选择有效的价格省份', {icon: 5, time: 2000});
        return
    }
    $("#areaName").html( $("#areaId").attr('porvinname'));
    $("#areaId").html($("#areaId").attr('porvincode'));
    $("#selectInp").css({
        display: "none"
    });
}

function area_cancel() {
    $("#selectInp").css({
        display: "none"
    });
}

/*----------------------区域获取结束------------------------*/
var dataSource = new Array();
//table的数据源
var table; //table对象
dataSource = [];

function goodsList(data) {
    if (dataSource.length > 0) {
        dataSource.splice(0, dataSource.length); //清空数组
    }
    if (data.data.list != null) {
        $.each(data.data.list, function (i, field) {
            var rowData = new Array();
            rowData.push(field.sku_code);//1 Code码
            rowData.push(field.img_url);//2 图片
            rowData.push(field.sku_name);//3 商品名称
            rowData.push(field.specifications);//4 规格属性
            rowData.push(field.brand_name);//5 品牌
            rowData.push(field.cost_price + "元" + (field.unit_name ? "/" + field.unit_name :""));//6 成本价
            rowData.push(field.price + "元" + (field.unit_name ? "/" + field.unit_name :""));//7 现金价
            rowData.push(field.distribution_price + "元" + (field.unit_name ? "/" + field.unit_name :""));//8 分销价
            rowData.push(field.retail_price + "元" + (field.unit_name ? "/" + field.unit_name :""));//9 售价
            rowData.push(field.show_price ? field.show_price + "元" + (field.unit_name ? "/" + field.unit_name :"") : 0);//10 限制展示价
            rowData.push(field.limit_lowest_price ? field.limit_lowest_price + "元" + (field.unit_name ? "/" + field.unit_name :"") : 0);//11 限制最低售价
            dataSource.push(rowData);
        });
    }
    initDataTable(dataSource);

    function initDataTable(dataSource) {
        var dttable = $('#datatable').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#datatable").DataTable({
            "autoWidth": false,
            "ordering": false,
            "searching": false,
            "paging": false,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu": [50, 25, 10, 5],
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
                "sProcessing": "<img src='./loading.gif' />"
            },
            data: dataSource,
            columnDefs: [
                {
                    "visible": true,
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        if (row[2] != null) {
                            var val = '<img src="' + row[1] + '" style="width:55px;height:55px;" onclick="picShow(\'查看图片\',\'../goods/picShow.html\',\'' + row[1] + '\',\'500\',\'300\',\'110\')"/>';
                            return val;
                        }
                        var val = '';
                        return val;

                    },
                    "targets": 1
                }
            ]
        });
    };
    $('#datatable tr').each(function () {
        // $(this).children('td').eq(10).css('text-align', 'left');
    })
}


function reloadSpuTable() {
    var val1=0;
    if($("#is_show_price").is(":checked")){
        val1=1;
    }else {
        val1=0;
    }

    var val2=0;
    if($("#is_tran_price").is(":checked")){
        val2=1;
    }else {
        val2=0;
    }

    var val3 = 0;
    if($("#is_low_cost_price").is(":checked")){
        val3=1;
    }

    var categoryName = $("#class3").val();
    var firstCategoryName = $("#class1").val();
    var secondCategoryName = $("#class2").val();
    if (categoryName == "" || categoryName == undefined || categoryName == null) {
        goodslist_cantegory_id = null;
        thirdCategoryId = null;
    }

    if (firstCategoryName == "" || firstCategoryName == undefined || firstCategoryName == null) {
        firstCategoryId = null;
    }

    if (secondCategoryName == "" || secondCategoryName == undefined || secondCategoryName == null) {
        secondCategoryId = null;
    }
    $.ajax({
        contentType: 'application/json',
        url: root.goodsInter.goods_findSkuList,
        // url: 'http://localhost:7050/aproduct/price/findSkuList',
        cache: false, //禁用缓存
        type: "POST",
        data: JSON.stringify({
            pageSize: 50,
            nowPage: 1,
            model: {
                areaId: $("#areaId").attr('porvincode'),
                areaName: $("#areaName").html(),
                platformType: $("#ordinaryId").val(),
                firstCategoryId: _categoryData.categoryId1,
                secondCategoryId: _categoryData.categoryId2,
                thirdCategoryId: _categoryData.categoryId3,
                categoryId: _categoryData.categoryId4,
                spuCode: $("#spu_code").val(),
                skuCode: $("#Code").val(),
                // productCode: $("#Code").val(),
                helpCode: $("#helpcode").val(),
                brandName: $("#brandName").val(),
                skuName: $("#goodsName").val(),
                specifications: $("#specifications").val(),
                aliasName: $("#aliasParam").val(),
                salesManParam: $("#salesManParam").val(),
                is_show_price:val1,
                is_tran_price:val2,
                is_low_cost_price:val3,
            }
        }),
        success: function (data) {
            $("#hiddenId").val('1');
            $(".bj").css("background-color", "#ffcc00");
            goodsList(data);
            totalPage = data.data.totalPage;
            $('.M-box3').pagination({
                pageCount: totalPage,
                jump: true,
                coping: true,
                prevContent: '上页',
                nextContent: '下页',
                callback: function (api) {
                    console.log(api.getCurrent());
                    $.ajax({
                        contentType: 'application/json',
                        // url: 'http://localhost:7050/aproduct/price/findSkuList',
                        url: root.goodsInter.goods_findSkuList,
                        cache: false, //禁用缓存
                        type: "POST",
                        data: JSON.stringify({
                            pageSize: 50,
                            nowPage: api.getCurrent(),
                            model: {
                                areaId: $("#areaId").html(),
                                areaName: $("#areaName").html(),
                                platformType: $("#ordinaryId").val(),
                                categoryId: goodslist_cantegory_id,
                                firstCategoryId: firstCategoryId,
                                secondCategoryId: secondCategoryId,
                                spuCode: $("#spu_code").val(),
                                skuCode: $("#Code").val(),
                                // productCode: $("#Code").val(),
                                helpCode: $("#helpcode").val(),
                                brandName: $("#brandName").val(),
                                skuName: $("#goodsName").val(),
                                specifications: $("#specifications").val(),
                                aliasName: $("#aliasParam").val(),
                                salesManParam: $("#salesManParam").val(),
                                is_show_price:val1,
                                is_tran_price:val2,
                                is_low_cost_price:val3,
                            }
                        }),
                        success: function (data) {
                            goodsList(data);
                        }
                    });
                }
            });
        }
    });
}

/*/!*-------------------------品类查询开始---------------------*!/
function goods_listCategory(categoryLevel) {
    console.log($("#class1").val())
    var categoryName = $("#class1").val();
    var user_data = {
        categoryLevel: categoryLevel,
        categoryName: $("#class1").val(),
    }
    var succCallBack = function (data, status, response) {
        console.log(data)
        if (data.errorCode == "0") {
            if (data.data == null) {
                classStr = "<li>无数据</li>"
                $("#classUl1").html(classStr);
            } else {
                var classStr = "";
                for (var i = 0; i < data.data.length; i++) {
                    classStr += "<li onclick='nextClass(" + data.data[i].category_id + "," + data.data[i].category_level + ",\"" + data.data[i].category_name + "\")'>" + data.data[i].category_name + "</li>"
                }
                $("#classUl1").html(classStr);
            }
        }
    }
    web.doAjax(root.goodsInter.categoryByParam, 'post', user_data, succCallBack);
}

//获取光标
$("#class1").focus(function () {
    $("#classUl1").show();
    goods_listCategory(1);
});
$(document).bind("click", function (e) {
    //id为menu的是菜单，id为open的是打开菜单的按钮
    if ($(e.target).closest("#classUl1").length == 0 && $(e.target).closest("#class1").length == 0) {
        //点击id为menu之外且id不是不是open，则触发
        $("#classUl1").hide()
        $("#class2").val();

    }
});

var goodslist_cantegory_id; //定义品类搜索条件

var firstCategoryId;//一级品类
var secondCategoryId;//二级品类
var thirdCategoryId;//三级品类
var fourthCategoryId;//四级品类

// 2,3,4级的模糊查询
function getNext_input(level, category_id) {
    if (level == 5) {
        goodslist_cantegory_id = category_id;
        fourthCategoryId = category_id;
    }
    if (level < 5) {
        console.log(level)
        $("#class" + level + "")[0].oninput = function () {
            var user_data = {
                parentCateId: category_id,
                categoryName: $("#class" + level + "").val()
            }
            var succCallBack = function (data, status, response) {
                if (data.errorCode == 0) {
                    if (data.data == null) {
                        classStr = "<li>无数据</li>";
                    } else {
                        var classStr = "";
                        for (var i = 0; i < data.data.length; i++) {
                            classStr += "<li onclick='nextClass(" + data.data[i].category_id + "," + data.data[i].category_level + ",\"" + data.data[i].category_name + "\")'>" + data.data[i].category_name + "</li>"
                        }
                    }
                    if (data.data[0] == undefined) {
                        classValue = category_id;
                        var nextLevel = level + 1;
                        $("#classUl" + nextLevel + "").html(classStr);
                        $("#classUl" + level + "").hide();
                    } else {
                        var nextLevel = data.data[0].category_level;
                        $("#classUl" + nextLevel + "").html(classStr);
                        $("#classUl" + level + "").show();
                    }
                }
            }
            web.doAjax(root.goodsInter.categoryByParam, 'post', user_data, succCallBack);
        }
        //获取光标
        $("#class" + level + "").focus(function () {
            $("#classUl" + level + "").show();
        });
        $(document).bind("click", function (e) {
            //id为menu的是菜单，id为open的是打开菜单的按钮
            if ($(e.target).closest("#classUl" + level + "").length == 0 && $(e.target).closest("#class" + level + "").length == 0) {
                //点击id为menu之外且id不是不是open，则触发
                $("#classUl" + level + "").hide()
            }
        });
    }

}

function nextClass(category_id, level, category_name) {
    $("#class" + level + "").val(category_name);
    if (level == 1) {
        firstCategoryId = category_id;
        $("#class2").val("");
        $("#class3").val("");
        $("#class4").val("");
    } else if (level == 2) {
        secondCategoryId = category_id;
        $("#class3").val("");
        $("#class4").val("");
    } else if (level == 3) {
        thirdCategoryId = category_id;
        $("#class4").val("");
    }
    var user_data = {
        parentCateId: category_id,
    }
    var succCallBack = function (data, status, response) {
        console.log(data)
        if (data.errorCode == "0") {
            if (data.data == null) {
                classStr = "<li>无数据</li>";
            } else {
                var classStr = "";
                for (var i = 0; i < data.data.length; i++) {
                    classStr += "<li onclick='nextClass(" + data.data[i].category_id + "," + data.data[i].category_level + ",\"" + data.data[i].category_name + "\")'>" + data.data[i].category_name + "</li>"
                }
            }
            if (data.data[0] == undefined) {
                classValue = category_id;
                var nextLevel = level + 1;
                $("#classUl" + nextLevel + "").html(classStr);
                $("#classUl" + level + "").hide();
            } else {
                var nextLevel = data.data[0].category_level;
                $("#classUl" + nextLevel + "").html(classStr);
                $("#classUl" + nextLevel + "").show();
                $("#classUl" + level + "").hide()
            }
        }
    }
    getNext_input(level + 1, category_id);
    web.doAjax(root.goodsInter.categoryByParam, 'post', user_data, succCallBack);
}

/!*----------------------品类查询结束------------------------*!/*/

/*----------------------区域获取开始------------------------*/



/*----------------------区域获取结束------------------------*/

/*设置筛选的数据高亮显示*/
function dataHighlighting() {
    $("#screenId").click(function () {
        //判断datatable是否存在数据
        var rows = $("#datatable tr").length;
        if (rows == 1) {
            $.Huimodalalert('没有数据不可筛选', 2000);
            return;
        }

        //获取背景色
        var $c = $(".isSpecifyPrice").css("background-color");
        if ($c == 'rgb(255, 255, 255)') {
            //设置背景色
            $(".isSpecifyPrice").css("background-color", '#ffcc00');
            //获取文字隐藏属性
            $(".fontDisplay").show();
        } else {
            $(".isSpecifyPrice").css("background-color", 'rgb(255, 255, 255)');
            //获取文字隐藏属性
            $(".fontDisplay").hide();
        }

    });
}

/*点击图片放大*/
function picShow(title, url, imgUrl) {
    window.event ? window.event.cancelBubble = true : e.stopPropagation();
    sessionStorage.setItem("imgUrl", imgUrl);
    layer_show(title, url, "800", "500");
}